#include				"RotatingBuffer.h"

RotatingBuffer::RotatingBuffer(int capacity)
{
	_capacity = capacity;
	_data.resize(capacity);
	_read = 0;
	_write = 0;
}

RotatingBuffer::~RotatingBuffer()
{

}

void						RotatingBuffer::init()
{
	_read = 0;
	_write = 0;
}

byte						RotatingBuffer::read()
{
	_mutex.lock();
	byte				ret = _data[_read];
	_read++;
	_read = _read % _data.size();
	_mutex.unlock();
	return ret;
}

void						RotatingBuffer::write(byte ret)
{
	_mutex.lock();
	_data[_write] = ret;
	_write++;
	_write = _write % _data.size();
	_mutex.unlock();
}

bool						RotatingBuffer::empty()
{
	return _read == _write;
}

int							RotatingBuffer::size()
{
	if (_write < _read)
		return (_write + _capacity) - _read;
	return (_write - _read);
}

int							RotatingBuffer::capacity()
{
	return _data.size();
}

byte						RotatingBuffer::operator[](size_t size)
{
	return (_data[_write + size]);
}